﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class ShippingDetailReward : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Username"] != null && Session["ID"] != null)
            {
                Panel1.Visible = true;
                lblUserName.Text = Session["Username"].ToString();
                lblUserID.Text = Session["ID"].ToString();
                lblUserID.Visible = false;
            }
            else
                Panel1.Visible = false;

            if (!IsPostBack)
            {
                SqlCommand cmdUser;
                SqlDataReader dtrUser;
                conn.Open();

                cmdUser = new SqlCommand("Select * From Users where u_id = @ID", conn);
                cmdUser.Parameters.AddWithValue("@ID", Session["ID"].ToString());
                dtrUser = cmdUser.ExecuteReader();
                dtrUser.Read();

                txtName.Text = dtrUser["username"].ToString();
                txtPhone.Text = dtrUser["u_phone"].ToString();
                txtEmail.Text = dtrUser["u_email"].ToString();

                string[] lines = dtrUser["u_address"].ToString().Split('|');
                txtAdd1.Text = lines[0].ToString();
                txtAdd2.Text = lines[1].ToString();
                txtPostCode.Text = lines[2].ToString();
                txtCity.Text = lines[3].ToString();
                txtState.Text = lines[4].ToString();
                txtCountry.Text = lines[5].ToString();

                dtrUser.Close();
                conn.Close();
            }
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RadioButtonList1.SelectedIndex == 0)
            {
                Label3.Visible = false;
                PanelBilling.Visible = false;
            }
            else
            {
                PanelBilling.Visible = true;
                Label3.Visible = true;

            }
        }

        protected void btnDelivery_Click(object sender, EventArgs e)
        {
            SqlCommand cmdSearchID, cmdInsert1, cmdUpdate1;
            String fullAddress, Email, PhoneNo, Recipient;

            if (PanelDelivery.Visible == true && PanelBilling.Visible == false)
            {
                fullAddress = txtAdd1.Text + "|" + txtAdd2.Text + "|" + txtPostCode.Text + "|" + txtCity.Text + "|" + txtState.Text + "|" + txtCountry.Text;
                Email = txtEmail.Text;
                PhoneNo = txtPhone.Text;
                Recipient = txtName.Text;
            }
            else
            {
                fullAddress = txtOptionAdd1.Text + "|" + txtOptionAdd2.Text + "|" + txtOptionPostCode.Text + "|" + txtOptionCity.Text + "|" + txtOptionState.Text + "|" + txtOptionCountry.Text;
                Email = txtOptionEmail.Text;
                PhoneNo = txtOptionPhone.Text;
                Recipient = txtOptionName.Text;
            }
            conn.Open();

            cmdSearchID = new SqlCommand("Select MAX(s_id) from Shipping_Details", conn);
            int s_id = Convert.ToInt32(cmdSearchID.ExecuteScalar());
            s_id++;

            cmdInsert1 = new SqlCommand("Insert Into Shipping_Details Values(@SID,@Sphone,@Recipient,@Address,@Email)", conn);
            cmdInsert1.Parameters.AddWithValue("@SID", s_id);
            cmdInsert1.Parameters.AddWithValue("@Sphone", PhoneNo);
            cmdInsert1.Parameters.AddWithValue("@Recipient", Recipient);
            cmdInsert1.Parameters.AddWithValue("@Address", fullAddress);
            cmdInsert1.Parameters.AddWithValue("@Email", Email);
            cmdInsert1.ExecuteNonQuery();

            cmdUpdate1 = new SqlCommand("Update Transactions SET s_id=@SID, t_status='RewardShipping' WHERE t_id=@TID", conn);
            cmdUpdate1.Parameters.AddWithValue("@SID", Convert.ToInt32(s_id));
            cmdUpdate1.Parameters.AddWithValue("@TID", Session["RewardReference"]);
            cmdUpdate1.ExecuteNonQuery();

            SqlCommand cmdSelect = new SqlCommand("SELECT * FROM Transactions t, Transactions_Products tp, users u WHERE t.t_id=tp.t_id AND t.u_id=u.u_id AND t.t_id='" + Session["RewardReference"] +"'", conn);
            SqlDataReader dtrSelect = cmdSelect.ExecuteReader();
            dtrSelect.Read();

            int p_id = Convert.ToInt32(dtrSelect["p_id"]);

            cmdUpdate1 = new SqlCommand("UPDATE Users SET u_points=@point WHERE u_id='"+ Session["id"] +"'", conn);
            int points = Convert.ToInt32(dtrSelect["u_points"]) - Convert.ToInt32(dtrSelect["t_total"]);
            cmdUpdate1.Parameters.AddWithValue("@point", points);
            dtrSelect.Close();
            cmdUpdate1.ExecuteNonQuery();

            cmdSelect = new SqlCommand("SELECT r_quantity FROM Reward WHERE r_id='" + p_id + "'", conn);
            int quantity = Convert.ToInt32(cmdSelect.ExecuteScalar()) - 1;

            cmdUpdate1 = new SqlCommand("UPDATE Reward SET r_quantity='" + quantity + "' WHERE r_id='" + p_id + "'", conn);
            cmdUpdate1.ExecuteNonQuery();

            conn.Close();
        }
    }
}